package com.xxx.springboot.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 测试 ClassLoader
 */
@Controller
@RequestMapping(value = "/", produces = "application/json;charset=utf-8")
public class TestClassLoaderControlloer {

	@RequestMapping("/testClassLoader1")
    @ResponseBody
    public Map<String, ClassLoader> testClassLoader1(String name1) throws ClassNotFoundException {
		Map<String, ClassLoader> map = new HashMap<>();
		
		// 当前类的ClassLoader
		ClassLoader loader1 = TestClassLoaderControlloer.class.getClassLoader();
		System.out.println("TestParentLoader class loader:" + loader1);
		map.put("TestParentLoader class loader", loader1);
		 
		// 当前类的内部类的ClassLoader（注意成员内部类的name）
		ClassLoader loader2 = Class.forName("com.xxx.springboot.controller.TestClassLoader$User").getClassLoader();
		System.out.println("TestClassLoader$User class loader:" + loader2);
		map.put("TestClassLoader$User class loader", loader1);

		// 当前线程的上下文的ClassLoader
		ClassLoader loader3 = Thread.currentThread().getContextClassLoader();
        System.out.println("currentThread.Context class loader:" + loader3);
        map.put("currentThread.Context class loader", loader1);
        
        // bootstrap ClassLoader
        ClassLoader loader4 = String.class.getClassLoader();
        System.out.println("String class loader:" + loader4);
        map.put("String class loader", loader1);
		
		return map;
    }
	
	class User {
    	private long id;
    	private String name;
    	public Integer age;

    	public long getId() {
    		return id;
    	}

    	public void setId(long id) {
    		this.id = id;
    	}

    	public String getName() {
    		return name;
    	}

    	public void setName(String name) {
    		this.name = name;
    	}

    	public Integer getAge() {
    		return age;
    	}

    	public void setAge(Integer age) {
    		this.age = age;
    	}

		@Override
		public String toString() {
			return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
		}
    }
	
}
